﻿<UserControl
    x:Class="BuildSkull.BuildSkullControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    Background="{DynamicResource VsBrush.Window}"
    Foreground="{DynamicResource VsBrush.WindowText}"
    xmlns:converters="clr-namespace:BuildSkull.Converters"
    xmlns:contracts="clr-namespace:BuildSkull.Contracts;assembly=BuildSkull.Contracts"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300"
    Name="MyToolWindow">

    <UserControl.Resources>

        <Style x:Key="StatusEllipseStyle" TargetType="Ellipse">
            <Setter Property="Width" Value="14" />
            <Setter Property="Height" Value="14" />
            <Setter Property="Stroke" Value="Gray" />
            <Setter Property="StrokeThickness" Value="0.5" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.None}">
                    <Setter Property="Fill" Value="LightGray" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.InProgress}">
                    <Setter Property="Fill" Value="LightBlue" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.Succeeded}">
                    <Setter Property="Fill" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.Failed}">
                    <Setter Property="Fill" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.Stopped}">
                    <Setter Property="Fill" Value="Orange" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BuildStatus, Mode=OneWay}" Value="{x:Static contracts:BuildStatusType.NotStarted}">
                    <Setter Property="Fill" Value="LightGreen" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="TextBlockStyleBase" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Segoe UI" />
            <Setter Property="FontSize" Value="14" />
        </Style>

        <HierarchicalDataTemplate x:Key="BuildCatalogItemTemplate" ItemsSource="{Binding Path=Children, Mode=OneWay}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Ellipse
                    Grid.Column="0"
                    Margin="2,0,5,0"
                    Width="10" Height="10"
                    Style="{StaticResource StatusEllipseStyle}">
                </Ellipse>
                <TextBlock
                    Grid.Column="1"
                    Style="{StaticResource TextBlockStyleBase}"
                    Text="{Binding Path=Name, Mode=OneWay}">
                </TextBlock>
            </Grid>
        </HierarchicalDataTemplate>
        
        <DataTemplate x:Key="BuildDefinitionItemTemplate">
            <Grid>
                <ContextMenuService.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Run" Command="{Binding Path=RunCommand}" />
                        <MenuItem Header="Stop" Command="{Binding Path=StopCommand}" />
                    </ContextMenu>
                </ContextMenuService.ContextMenu>
                <ToolTipService.ToolTip>
                    <TextBlock>
                        <Run Text="Last Committer:" />
                        <Run Text="{Binding Path=Model.LastRun.CommittedBy.Name, TargetNullValue='-not run-', FallbackValue='-not run-'}" />
                        <LineBreak />
                        <Run Text="Build Finished:" />
                        <Run Text="{Binding Path=Model.LastRun.FinishTime, TargetNullValue='-not run-', FallbackValue='-not run-'}" />
                    </TextBlock>
                </ToolTipService.ToolTip>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Ellipse
                    Grid.Column="0"
                    Margin="0,0,5,0"
                    Style="{StaticResource StatusEllipseStyle}">
                </Ellipse>
                <TextBlock
                    Grid.Column="1"
                    Style="{StaticResource TextBlockStyleBase}"
                    Text="{Binding Path=Name, Mode=OneWay}">
                </TextBlock>
            </Grid>
        </DataTemplate>

        <converters:BuildItemDataTemplateSelector
            x:Key="BuildItemDataTemplateSelector"
            BuildCatalogTemplate="{StaticResource BuildCatalogItemTemplate}"
            BuildDefinitionTemplate="{StaticResource BuildDefinitionItemTemplate}">
        </converters:BuildItemDataTemplateSelector>
        
    </UserControl.Resources>

    <Grid>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock
            Grid.Row="0" Margin="10,10,10,10" HorizontalAlignment="Left">
            <Hyperlink Command="{Binding Path=RefreshItemsCommand}">
                <Run Text="Refresh" />
            </Hyperlink>
        </TextBlock>

        <TextBlock
            Grid.Row="0" Margin="10,10,10,10" HorizontalAlignment="Right">
            <Hyperlink Click="OnExpandCollapseClick">
                <Run Text="Collapse/Expand All" />
            </Hyperlink>
        </TextBlock>
        
        <TreeView
            x:Name="ItemsTreeView"
            Grid.Row="1" Margin="10,10,0,10"
            ItemsSource="{Binding Path=Items, Mode=OneWay}"
            ItemTemplateSelector="{StaticResource BuildItemDataTemplateSelector}"
            Background="{x:Null}" BorderThickness="0" BorderBrush="{x:Null}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>

    </Grid>
</UserControl>
